#include "StdAfx.h"
#include "NavigatorButtonUI.h"
#include "Rect.h"

CNavigatorButtonUI::CNavigatorButtonUI(void): m_uButtonState(0)
{
}

CNavigatorButtonUI::~CNavigatorButtonUI(void)
{
}

LPCTSTR CNavigatorButtonUI::GetClass() const
{
	return _T("NavigatorButton");
}

void CNavigatorButtonUI::Event(TEventUI& event)
{
	if( event.Type == UIEVENT_BUTTONDOWN && IsEnabled() )
	{
		RECT rcButton = GetButtonRect(m_rcItem);
		if( ::PtInRect(&rcButton, event.ptMouse) ) {
			m_uButtonState |= UISTATE_PUSHED | UISTATE_CAPTURED;
			Invalidate();
		}
	}
	if( event.Type == UIEVENT_MOUSEMOVE )
	{
		if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) {
			RECT rcButton = GetButtonRect(m_rcItem);
			if( ::PtInRect(&rcButton, event.ptMouse) ) m_uButtonState |= UISTATE_PUSHED;
			else m_uButtonState &= ~UISTATE_PUSHED;
			Invalidate();
		}
	}
	if( event.Type == UIEVENT_BUTTONUP )
	{
		if( (m_uButtonState & UISTATE_CAPTURED) != 0 ) {
			RECT rcButton = GetButtonRect(m_rcItem);
			if( ::PtInRect(&rcButton, event.ptMouse) ) {
				m_pManager->SendNotify(this, _T("link"));
				Select();
			}
			m_uButtonState &= ~(UISTATE_PUSHED | UISTATE_CAPTURED);
			Invalidate();
		}
	}
}

SIZE CNavigatorButtonUI::EstimateSize(SIZE szAvailable)
{
	return CStdSize(0, 18 + m_pManager->GetThemeFontInfo(UIFONT_NORMAL).tmHeight);
}

void CNavigatorButtonUI::DoPaint(HDC hDC, const RECT& rcPaint)
{
	DrawItem(hDC, m_rcItem, 0);
}

void CNavigatorButtonUI::DrawItem(HDC hDC, const RECT& rcItem, UINT uStyle)
{
	RECT rcButton = GetButtonRect(m_rcItem);

	if( (m_uButtonState & UISTATE_PUSHED) != 0 ) {
		CBlueRenderEngineUI::DoFillRect(hDC, m_pManager, rcButton, UICOLOR_NAVIGATOR_BUTTON_PUSHED);
	}
	else if( m_bSelected ) {
		CBlueRenderEngineUI::DoFillRect(hDC, m_pManager, rcButton, UICOLOR_NAVIGATOR_BUTTON_SELECTED);
	}
	else if( (m_uButtonState & UISTATE_PUSHED) != 0 ) {
		CBlueRenderEngineUI::DoFillRect(hDC, m_pManager, rcButton, UICOLOR_NAVIGATOR_BUTTON_HOVER);
	}
	::SelectObject(hDC, m_pManager->GetThemePen(m_bSelected ? UICOLOR_NAVIGATOR_BORDER_SELECTED : UICOLOR_NAVIGATOR_BORDER_NORMAL));
	POINT ptTemp = { 0 };
	::MoveToEx(hDC, rcButton.left, rcButton.top, &ptTemp);
	::LineTo(hDC, rcButton.left, rcButton.bottom);
	::LineTo(hDC, rcButton.right, rcButton.bottom);

	UITYPE_COLOR iTextColor = UICOLOR_NAVIGATOR_TEXT_NORMAL;
	if( (m_uButtonState & UISTATE_PUSHED) != 0 ) iTextColor = UICOLOR_NAVIGATOR_TEXT_PUSHED;
	else if( m_bSelected ) iTextColor = UICOLOR_NAVIGATOR_TEXT_SELECTED;

	RECT rcText = rcButton;
	::OffsetRect(&rcText, 0, 1);
	::InflateRect(&rcText, -8, 0);
	if( (m_uButtonState & UISTATE_PUSHED) != 0 ) {
		rcText.left++; 
		rcText.top += 2;
	}
	int nLinks = 0;
	CBlueRenderEngineUI::DoPaintPrettyText(hDC, m_pManager, rcText, m_sText, iTextColor, UICOLOR__INVALID, NULL, nLinks, DT_SINGLELINE | DT_VCENTER);
}

RECT CNavigatorButtonUI::GetButtonRect(RECT rc) const
{
	int cy = m_pManager->GetThemeFontInfo(UIFONT_NORMAL).tmHeight + 8;
	return CStdRect(rc.left + 10, rc.top, rc.right, rc.top + cy);
}

